package chartCraft.graphDesk;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;


import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import chartCraft.ChartCraftController;
import chartCraft.Log;
import chartCraft.model.Model;
import chartCraft.model.Model.NotifyingMessage;
import chartCraft.model.TopModel;

public class DraggableBackground extends DraggableItem {

	private static final long serialVersionUID = 1L;
	private TopModel mModel;
	private final ChartCraftController mChartCraftController;
	
	public DraggableBackground(ChartCraftController chartCraftController, TopModel model) {
		super();
				
		mChartCraftController = chartCraftController;
		setModel(model);
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g); 
		
		Image image = mModel.getBackgroundImage();
		Dimension size = getSize();
		g.drawImage(image, 0, 0, size.width, size.height, null);
	
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		if (arg0 == mModel) {
			
			NotifyingMessage msg = (NotifyingMessage) arg1;
			String action = msg.getAction();
			
			
			if (action == TopModel.BACKGROUND_DATA_UPDATED) {
				repaint();
				
			} else if (action == TopModel.BACKGROUND_MOVED) {
				Point newLocation = (Point) msg.getObject();
				moveTo(newLocation);
				
			} else if(action == TopModel.BACKGROUND_RESIZED) {
				Dimension newSize = (Dimension) msg.getObject();
				resizeTo(newSize);
				
			} else if(action == TopModel.BACKGROUND_SELECTION_STATUS) {
				boolean isSelect = (Boolean) msg.getObject();
				showResizableBorder(isSelect);
			}
			 
		}
	}

	@Override
	public void setModel(Model model) {
		mModel = (TopModel) model;
		model.addGraphViewObserver(this);
		
		reloadFromModel();
		
	}

	private void reloadFromModel() {
		moveTo(mModel.getBackgroundLocation());
		resizeTo(mModel.getBackgroundSize());
		repaint();
	}

	@Override
	public TopModel getModel() {
		return mModel;
	}

	@Override
	protected void beingSelected() {
		Image image = mModel.getBackgroundImage();
		if (image != null) {
			//have background
			mChartCraftController.selectBackgroundOnly();
		}
	}

	@Override
	public void draggingEnd(Point startPoint, Point endPoint, Dimension startSize, Dimension endSize) {
		Image image = mModel.getBackgroundImage();
		if (image != null) {
			//have background
			mChartCraftController.moveAndResizeBackground(startPoint, endPoint, startSize, endSize);
		}
	}

	@Override
	public JPopupMenu getPopUpMenu() {
		JPopupMenu result = new JPopupMenu();
		
		JMenuItem changeItem = new JMenuItem("Change...");
		result.add(changeItem);
		changeItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mChartCraftController.changeBackgroundImage();				
			}
		});
		
		JMenuItem removeItem = new JMenuItem("Remove");
		result.add(removeItem);
		removeItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mChartCraftController.deleteBackgroundImage();				
			}
		});
		
		JMenuItem hideItem = new JMenuItem("Hide");
		result.add(hideItem);	
		hideItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mChartCraftController.setImageHidding(true);				
			}
		});
		return result;
	}

}
